<template>
	<view class="content-box">
		<u-notify ref="uNotify"></u-notify>
		<u-loading-page :loading="isLoading"></u-loading-page>
		<view class="" style="width: 100%;">
			<u--form
				labelPosition="left"
				:model="data"
				ref="uForm">
				<u-collapse
					:value="['base']">
					<u-collapse-item
						title="基础信息"
						name="base">
						<u-form-item
							label="名称:"
							borderBottom>
							<u--input v-model="data.name" border="none"></u--input>
						</u-form-item>
						<u-form-item
							label="URL:"
							borderBottom>
							<u--input v-model="data.path" border="none"></u--input>
						</u-form-item>
						<u-form-item
							label="方法:"
							borderBottom>
							<text @click="methodShow = !methodShow"> {{ data.method }} </text>
							<u-picker :show="methodShow" :columns="methodTypes" @confirm="confirmMethod"></u-picker>
						</u-form-item>
						<u-form-item
							label="最近修改:"
							borderBottom>
							<text> {{ data.updateTime }} </text>
						</u-form-item>
					</u-collapse-item>
					<u-collapse-item
						title="说明"
						name="description">
						<u--textarea v-model="data.description" :autoHeight="true"></u--textarea>
					</u-collapse-item>
					<u-collapse-item
						title="代码"
						name="code">
						<u--textarea v-model="data.code" :autoHeight="true"></u--textarea>
					</u-collapse-item>
				</u-collapse>
				<view class="content-box-center">
					<u-button style="width: 70%; margin-top: 16px;" type="primary" text="保存" @click="save"></u-button>
					<u-button style="width: 70%; margin-top: 16px;" type="warning" text="发布" @click="release"></u-button>
					<u-button style="width: 70%; margin-top: 16px;" type="error" text="删除" @click="remove"></u-button>
				</view>
			</u--form>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: '',
				data: {},
				token: '',
				isLoading: false,
				methodShow: false,
				methodTypes: [
					["GET", "POST", "PUT", "DELETE"]
				]
			}
		},
		onShow() {
			this.getPageOptions()
		},
		methods: {
			getPageOptions() {
				const pages = getCurrentPages()
				const currentPage = pages[pages.length - 1]
				const options = currentPage.options
				this.id = options.id
				uni.getStorage({
					key: 'token',
					success: (token) => {
						this.token = token.data
						this.getItemData()
					},
					fail() {
						uni.clearStorage()
						uni.redirectTo({
							url: "/pages/index/index"
						})
					}
				})
			},
			getItemData() {
				this.isLoading = true
				uni.request({
					url: "/api/aop?id=" + this.id,
					method: "GET",
					header: {
						Authorization: this.token,
					},
					success: (res) => {
						if(res.data.code === 200) {
							this.data = res.data.data
						} else {
							this.$refs.uNotify.show({
								top: 10,
								type: 'error',
								color: '#ff0000',
								bgColor: '#2f3233',
								message: res.data.msg,
								duration: 1000 * 3,
								fontSize: 14,
								safeAreaInsetTop: false
							})
						}
						this.isLoading = false
					},
					fail: () => {
						this.$refs.uNotify.show({
							top: 10,
							type: 'error',
							color: '#ff0000',
							bgColor: '#2f3233',
							message: '服务器出错',
							duration: 1000 * 3,
							fontSize: 14,
							safeAreaInsetTop: false
						})
						this.isLoading = false
					},
				})
			},
			confirmMethod(e) {
				this.data.method = e.value[0]
				this.methodShow = false
			},
			save() {
				uni.request({
					url: "/api/aop",
					method: "POST",
					header: {
						Authorization: this.token,
					},
					data: this.data,
					success: (res) => {
						if(res.data.code === 200){
							this.$refs.uNotify.show({
								top: 10,
								type: 'success',
								color: '#00f600',
								bgColor: '#2f3233',
								message: res.data.msg,
								duration: 1000 * 3,
								fontSize: 14,
								safeAreaInsetTop: false
							})
						} else {
							this.$refs.uNotify.show({
								top: 10,
								type: 'error',
								color: '#ff0000',
								bgColor: '#2f3233',
								message: res.data.msg,
								duration: 1000 * 3,
								fontSize: 14,
								safeAreaInsetTop: false
							})
						}
					},
					fail() {
						this.$refs.uNotify.show({
							top: 10,
							type: 'error',
							color: '#ff0000',
							bgColor: '#2f3233',
							message: '服务器出错',
							duration: 1000 * 3,
							fontSize: 14,
							safeAreaInsetTop: false
						})
					}
				})
			},
			release() {
				uni.request({
					url: "/api/aop/release/" + this.id,
					method: "GET",
					header: {
						Authorization: this.token,
					},
					success: (res) => {
						if(res.data.code === 200){
							this.$refs.uNotify.show({
								top: 10,
								type: 'success',
								color: '#00f600',
								bgColor: '#2f3233',
								message: res.data.msg,
								duration: 1000 * 3,
								fontSize: 14,
								safeAreaInsetTop: false
							})
						} else {
							this.$refs.uNotify.show({
								top: 10,
								type: 'error',
								color: '#ff0000',
								bgColor: '#2f3233',
								message: res.data.msg,
								duration: 1000 * 3,
								fontSize: 14,
								safeAreaInsetTop: false
							})
						}
					},
					fail() {
						this.$refs.uNotify.show({
							top: 10,
							type: 'error',
							color: '#ff0000',
							bgColor: '#2f3233',
							message: '服务器出错',
							duration: 1000 * 3,
							fontSize: 14,
							safeAreaInsetTop: false
						})
					}
				})
			},
			remove() {
				uni.request({
					url: "/api/aop?id=" + this.id,
					method: "DELETE",
					header: {
						Authorization: this.token,
					},
					success: (res) => {
						if(res.data.code === 200){
							this.$refs.uNotify.show({
								top: 10,
								type: 'success',
								color: '#00f600',
								bgColor: '#2f3233',
								message: res.data.msg,
								duration: 1000 * 3,
								fontSize: 14,
								safeAreaInsetTop: false
							})
							uni.navigateBack()
						} else {
							this.$refs.uNotify.show({
								top: 10,
								type: 'error',
								color: '#ff0000',
								bgColor: '#2f3233',
								message: res.data.msg,
								duration: 1000 * 3,
								fontSize: 14,
								safeAreaInsetTop: false
							})
						}
					},
					fail() {
						this.$refs.uNotify.show({
							top: 10,
							type: 'error',
							color: '#ff0000',
							bgColor: '#2f3233',
							message: '服务器出错',
							duration: 1000 * 3,
							fontSize: 14,
							safeAreaInsetTop: false
						})
					}
				})
			},
		}
	}
</script>

<style>

</style>
